package com.ct.controller;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ct.entity.po.Approval;
import com.ct.entity.query.ApprovalQuery;
import com.ct.entity.vo.ResponseVO;
import com.ct.service.ApprovalService;

import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotEmpty;

/**
 * 审批状态表 Controller
 */
@RestController("approvalController")
@RequestMapping("/approval")
public class ApprovalController extends ABaseController {

    @Resource
    private ApprovalService approvalService;

    /**
     * 根据条件分页查询
     */
    @RequestMapping("/loadDataList")
    public ResponseVO loadDataList(ApprovalQuery query) {
        return getSuccessResponseVO(approvalService.findListByPage(query));
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    public ResponseVO add(@NotEmpty String userId, @NotEmpty BigDecimal advancePayment) {
        System.out.println(advancePayment.doubleValue());
        Approval bean = null;

        if (advancePayment.doubleValue() > 0) {
            bean = new Approval(null, 0, 0, 0, 0, null, null, null, null, null, userId);
        } else {
            bean = new Approval(null, 0, 3, 0, 0, null, null, null, null, null, userId);
        }
        approvalService.add(bean);
        return getSuccessResponseVO(null);
    }

    /**
     * 批量新增
     */
    @RequestMapping("/addBatch")
    public ResponseVO addBatch(@RequestBody List<Approval> listBean) {
        approvalService.addBatch(listBean);
        return getSuccessResponseVO(null);
    }

    /**
     * 批量新增/修改
     */
    @RequestMapping("/addOrUpdateBatch")
    public ResponseVO addOrUpdateBatch(@RequestBody List<Approval> listBean) {
        approvalService.addBatch(listBean);
        return getSuccessResponseVO(null);
    }

    /**
     * 根据ApprovalId查询对象
     */
    @RequestMapping("/getApprovalByApprovalId")
    public ResponseVO getApprovalByApprovalId(Integer approvalId) {
        return getSuccessResponseVO(approvalService.getApprovalByApprovalId(approvalId));
    }

    /**
     * 根据ApprovalId修改对象
     */
    @RequestMapping("/updateApprovalByApprovalId")
    public ResponseVO updateApprovalByApprovalId(Approval bean, Integer approvalId) {
        approvalService.updateApprovalByApprovalId(bean, approvalId);
        return getSuccessResponseVO(null);
    }

    /**
     * 根据ApprovalId删除
     */
    @RequestMapping("/deleteApprovalByApprovalId")
    public ResponseVO deleteApprovalByApprovalId(Integer approvalId) {
        approvalService.deleteApprovalByApprovalId(approvalId);
        return getSuccessResponseVO(null);
    }

    /**
     * 分页查询出差申请
     * 
     * @param userId   工号
     * @param pageNo   页码
     * @param pageSize 页大小
     * @return 分页结果
     */
    @RequestMapping("/getTravelApply")
    public ResponseVO getTravelApply(@NotEmpty String userId, Integer pageNo, Integer pageSize) {
        System.out.println("getTravelApply");
        return getSuccessResponseVO(approvalService.getTravelApply(userId, pageNo, pageSize));
    }

    /**
     * 审批出差申请
     * 
     * @param userId          工号
     * @param approvalId      审批Id
     * @param status          审批状态
     * @param travelRejection 出差拒绝原因
     * @return 审批结果
     */
    @RequestMapping("approveTravelApply")
    public ResponseVO approveTravelApply(@NotEmpty String userId, @NotEmpty String approvalId,
            @NotEmpty String status, String travelRejection) {
        System.out.println("approveTravelApply" + userId + " " + approvalId + " " + status + " " + travelRejection);
        if (approvalService.approveTravelApply(userId, approvalId, status, travelRejection) > 0) {
            return getSuccessResponseVO(null);
        }
        return getServerErrorResponseVO(null);
    }

    /**
     * 分页查询出差申请
     * 
     * @param userId   工号
     * @param pageNo   页码
     * @param pageSize 页大小
     * @return 分页结果
     */
    @RequestMapping("/getAdvanceApply")
    public ResponseVO getAdvanceApply(@NotEmpty String userId, Integer pageNo, Integer pageSize) {
        System.out.println("getAdvanceApply");
        return getSuccessResponseVO(approvalService.getAdvanceApply(userId, pageNo, pageSize));
    }

    /**
     * 审批出差申请
     * 
     * @param userId           工号
     * @param approvalId       审批Id
     * @param payment          支付金额
     * @param status           审批状态
     * @param advanceRejection 出差拒绝原因
     * @return 审批结果
     */
    @RequestMapping("approveAdvanceApply")
    public ResponseVO approveAdvanceApply(@NotEmpty String userId, @NotEmpty String approvalId, String payment,
            @NotEmpty String status, String advanceRejection) {
        if (approvalService.approveAdvanceApply(userId, approvalId, payment, status, advanceRejection) > 0) {
            return getSuccessResponseVO(null);
        }
        return getServerErrorResponseVO(null);
    }

    /**
     * 分页查询报销申请
     * 
     * @param userId   工号
     * @param pageNo
     * @param pageSize 页大小
     * @return 分页结果
     */
    @RequestMapping("getReimbursementApplyMa")
    public ResponseVO getReimbursementApplyMa(@NotEmpty String userId, Integer pageNo, Integer pageSize) {
        System.out.println("getReimbursementApplyMa");
        return getSuccessResponseVO(approvalService.getReimbursementApplyMa(userId, pageNo, pageSize));
    }

    /**
     * 审批报销申请
     * 
     * @param userId                 工号
     * @param reimbursementId        报销Id
     * @param approvalId             审批Id
     * @param status                 审批状态
     * @param reimbursementRejection 报销拒绝原因
     * @return 审批结果
     */
    @RequestMapping("approveReimbursementApplyMa")
    public ResponseVO approveReimbursementApplyMa(@NotEmpty String userId, @NotEmpty String reimbursementId,
            @NotEmpty String approvalId, @NotEmpty String status, String reimbursementRejection) {
        System.out.println("approveReimbursementApplyMa" + userId + " " + reimbursementId + " " + approvalId + " "
                + status + " " + reimbursementRejection);
        if (approvalService.approveReimbursementApplyMa(userId, reimbursementId, approvalId, status,
                reimbursementRejection) > 0) {
            return getSuccessResponseVO(null);
        }
        return getServerErrorResponseVO(null);
    }

    /**
     * 分页查询报销申请
     * 
     * @param userId   工号
     * @param pageNo   页码
     * @param pageSize 页大小
     * @return 分页结果
     */
    @RequestMapping("getReimbursementApplyFi")
    public ResponseVO getReimbursementApplyFi(@NotEmpty String userId, Integer pageNo, Integer pageSize) {
        System.out.println("getReimbursementApplyFi");
        return getSuccessResponseVO(approvalService.getReimbursementApplyFi(userId, pageNo, pageSize));
    }

    /**
     * 审批报销申请
     * 
     * @param userId                 工号
     * @param reimbursementId        报销Id
     * @param approvalId             审批Id
     * @param payment                支付金额
     * @param status                 审批状态
     * @param reimbursementRejection 报销拒绝原因
     * @return 审批结果
     */
    @RequestMapping("approveReimbursementApplyFi")
    public ResponseVO approveReimbursementApplyFi(@NotEmpty String userId, @NotEmpty String reimbursementId,
            @NotEmpty String approvalId, String payment, @NotEmpty String status, String reimbursementRejection) {
        System.out.println("approveReimbursementApplyFi" + userId + " " + reimbursementId + " " + approvalId + " "
                + payment + " " + status + " " + reimbursementRejection);
        System.out.println("星期五测试输出的拒绝原因" + reimbursementRejection);
        if (approvalService.approveReimbursementApplyFi(userId, reimbursementId, approvalId, payment, status,
                reimbursementRejection) > 0) {
            return getSuccessResponseVO(null);
        }
        return getServerErrorResponseVO(null);
    }

    @PostMapping("getApprovalByTravelId") // 这里@RequestBody一定要
    public Approval getApprovalByTravelId(@RequestBody String travelId) {
        return approvalService.findApprovalByTravelId(Integer.parseInt(travelId));
    }

    @PostMapping("updateApprovalByTravelId") // 这里更新状态表中的经理审批状态为0
    public Integer updateApprovalByTravelId(@RequestBody String travelId) {
        Approval approval = approvalService.getApprovalByTravelId(Integer.valueOf(travelId));
        approval.setReimbursementApprovalMa(0);
        approvalService.updateApprovalByApprovalId(approval, approval.getApprovalId());
        return 1;
    }

    @RequestMapping("getApprovalByTravelId1")
    public ResponseVO getApprovalByTravelId1(@RequestParam String travelId) {
        System.out.println(travelId);
        System.out.println(approvalService.getApprovalByTravelId(Integer.parseInt(travelId)));
        return getSuccessResponseVO(approvalService.getApprovalByTravelId(Integer.parseInt(travelId)));
    }
}